package com.sdkj.recruit.controller;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.sdkj.recruit.model.ResultCode;
import com.sdkj.recruit.model.ResultData;
import com.sdkj.recruit.service.IAliyunSmsService;

@Controller
@Path("/sms")
public class AliyunSmsController {

	@Autowired
	private IAliyunSmsService smsService;
	
	@Path("/sendSms")
	@POST
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public ResultData<Object> sendSms(final String data, @Context HttpServletRequest request) {

		String checkCode = String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
		if(data ==null) {
			return ResultCode.error("参数不能为空", data);
		}
		JSONObject parma =JSONObject.parseObject(data);
		try {
			SendSmsResponse response = smsService.sendSms(parma.getString("phone"), checkCode,1);
			if (response.getCode() != null && response.getCode().equals("OK")) {
				return ResultCode.success("发送消息成功", response);
			}

		} catch (ClientException e) {
			return ResultCode.error(e.getErrMsg(), null);
		}
		return null;
	}
}
